import { createRouter, createWebHistory } from 'vue-router'
const routes = [{
    path: '/',
    name: 'login',
    component: () =>
        import('../views/Login.vue'),
    meta: {
        isShow: false
    }

}, {
    path: '/Home',
    name: 'Home',
    component: () =>
        import('../views/Home.vue'),
    meta: {
        isShow: false
    },
    // 配置子路由
    children: [
        {
            path: '/users',
            name: '/users',
            component: () => import('../views/children/Users.vue'),
            meta: { title: "用户列表", },
        },
        {
            path: '/roles',
            name: '/roles',
            component: () => import('../views/children/Roles.vue'),
            meta: { title: "角色列表", },
        },
        {
            path: '/rights',
            name: '/rights',
            component: () => import('../views/children/Rights.vue'),
            meta: { title: "权限列表", },
        },
        {
            path: '/goods',
            name: '/goods',
            component: () => import('../views/children/Goods.vue'),
            meta: { title: "商品列表", },
        },
        {
            path: '/params',
            name: '/params',
            component: () => import('../views/children/Params.vue'),
            meta: { title: "分类参数", },
        },
        {
            path: '/categories',
            name: '/categories',
            component: () => import('../views/children/Categories.vue'),
            meta: { title: "商品分类", },
        },
        {
            path: '/orders',
            name: '/orders',
            component: () => import('../views/children/Orders.vue'),
            meta: { title: "订单列表", },
        },
        {
            path: '/reports',
            name: '/reports',
            component: () => import('../views/children/Reports.vue'),
            meta: { title: "数据报表", },
        }, {
            path: '/index',
            name: '/index',
            component: () => import('../views/children/index.vue'),
            meta: { title: "首页", },
        },
    ]
},
//404 页面 
{
    path: '/:catchAll(.*)',
    name: '/404',
    component: () =>
        import('../views/404.vue'),
    meta: {
        isShow: false
    }
},
// 登录
{
    path: '/login',
    name: '/login',
    component: () =>
        import('../views/Login.vue'),
    meta: {
        isShow: false
    }
},
// 注册
{
    path: '/register',
    name: '/register',
    component: () =>
        import('../views/Register.vue'),
    meta: {
        isShow: false
    }
},


]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})
// 添加导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login'|| to.path === '/register' ) return next()
    const token = localStorage.getItem('token');
    if(!token) return next('/login')
    next()
})
export default router